package cn.edu.njpi.trip;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.trip
 * @date 2021/12/12 上午2:09
 */
public class TripDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 获取job对象
        Configuration conf = new Configuration();
//        conf.set("fs.defaultFS", "hdfs://hadoop101:9000");
//        System.setProperty("HADOOP_USER_NAME", "root");
        Job job = Job.getInstance(conf);

        // 关联driver类
        job.setJarByClass(TripDriver.class);

        // 关联mapper和reducer
        job.setMapperClass(TripMapper.class);
        job.setReducerClass(TripReducer.class);

        // 设置map端输出数据的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        // 设置程序最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // 指定自定义分区器
        job.setPartitionerClass(TripPartition.class);

        // 同时指定相应数量的ReduceTask
        job.setNumReduceTasks(3);

        // 设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}
